const express = require("express")
const router = express.Router()
const query = require('../db/mysql')

router.get('/xiaomi', (req, res) => {
    let { type,id } = req.query
    console.log(req.query);
    let sql = "select * from goods where type = ? or id = ?"
    query(sql,[type,id],(err,data)=>{
        if(err){
            res.send({
                code:-1,
                msg:err
            })
        }
        if(data.length){
            res.send({
                code:0,
                list:data
            })
        }else{
            res.send({
                code:1,
                msg:'没有数据'
            })
        }
    })
})

router.get('/search', (req, res) => {
    let { title,num } = req.query
    console.log(req.query);
    if(num == '1'){
        var sql = `select * from goods where title like '%${title}%' or type like '%${title}%' order by sales desc`
    }else if(num == '2'){
        var sql = `select * from goods where title like '%${title}%' or type like '%${title}%' order by price asc`
    }else if(num == '3'){
        var sql = `select * from goods where title like '%${title}%' or type like '%${title}%' order by price desc` 
    }else{
    var sql = `select * from goods where title like '%${title}%' or type like '%${title}%' order by id desc`
   }
    query(sql,(err,data)=>{
        if(err){
            res.send({
                code:-1,
                msg:'服务器错误'
            })
        }
        if(data.length){
            res.send({
                code:0,
                list:data
            })
        }else{
            res.send({
                code:1,
                msg:'没有数据'
            })
        }
    })
})

module.exports = router